package lbframework.bean.textArea;

import java.awt.Color;
import java.awt.KeyboardFocusManager;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.undo.UndoManager;
import lbframework.bean.field.LBPlainDocument;
import lbframework.classe.Util;
import lbframework.classe.LBException;

public class LBTextArea extends javax.swing.JTextArea {

    public final static String MASK_TEXTO = "Texto";
    private String mascara = "";
    private boolean caixaAlta = true;
    private boolean obrigatorio = false;
    private UndoManager undo = new UndoManager();
    private LBPlainDocument plain = new LBPlainDocument();

    public LBTextArea() {
        initComponents();

        this.setWrapStyleWord(true);

        this.mascara = MASK_TEXTO;
        plain.addUndoableEditListener(undo);
        setDocument(plain);

        //configura tab
        HashSet kTab = new HashSet(1);
        kTab.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));

        setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, kTab);

        HashSet kShiftTab = new HashSet(1);
        kShiftTab.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));

        setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, kShiftTab);

        setDisabledTextColor(Color.BLACK);
    }

    @Override
    public void setCaretPosition(int position) {
        super.setCaretPosition(position);
    }

    public void setCaixaAlta(boolean i_caixaAlta) {
        caixaAlta = i_caixaAlta;
        plain.setCaixaAlta(i_caixaAlta);
    }

    public boolean isCaixaAlta() {
        return caixaAlta;
    }

    public void clear() {
        this.setText("");
    }

    public boolean isObrigatorio() {
        return obrigatorio;
    }

    public void setObrigatorio(boolean obrigatorio) {
        this.obrigatorio = obrigatorio;

        if (this.isEnabled()) {
            if (obrigatorio) {
                this.setBackground(Util.COR_OBRIGATORIO);
            } else {
                if(this.isEditable()){
                    this.setBackground(UIManager.getColor("TextArea.background"));
                }
            }
        } else {
            this.setBackground(UIManager.getColor("TextArea.disabledBackground"));
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);

        setObrigatorio(obrigatorio);
    }

    @Override
    public void setEditable(boolean b) {
        super.setEditable(b);

        setObrigatorio(obrigatorio);
    }

    public String getMascara() {
        return mascara;
    }

    public void setMascara(String i_mascara) {
        try {
            mascara = i_mascara;

            if (i_mascara.equals(MASK_TEXTO)) {
                plain.setAcceptedChars(LBPlainDocument.ALPHA_NUMERIC);

            } else {
                plain.setAcceptedChars("");
            }

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
    }

    @Override
    public void setColumns(int columns) {
        super.setColumns(columns);

        plain.setColumns(columns);
    }

    @Override
    public void setBackground(Color bg) {
        super.setBackground(bg);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setFont(new java.awt.Font("Tahoma", 0, 11));
        setLineWrap(true);
        setWrapStyleWord(true);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                formKeyReleased(evt);
            }
        });
    }// </editor-fold>//GEN-END:initComponents

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_Z) {
            if (undo.canUndo()) {
                undo.undo();
            }
        } else if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_Y) {
            if (undo.canRedo()) {
                undo.redo();
            }
        }
    }//GEN-LAST:event_formKeyPressed

    private void formKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyReleased
    }//GEN-LAST:event_formKeyReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
